﻿using NB.MGJ.Common.API;
using NB.MGJ.Common.Utility;
using NB.MGJ.Model;
using NB.MGJ.Service.Origins;
using NB.MGJ.Service.Product;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using NB.MGJ.Common.OSS;
using NB.MGJ.Service.AntiFake;
using Common;

namespace NB.MGJ.API.API_WX
{
    [ApiAuthorizeFilter]
    [ApiHandleError]
    public class ApiMGJOriginsTaskController : ApiBaseController<LogOnUserModel>
    {
        public ProductBatchService _ProductBatchService = new ProductBatchService();
        public OriginsNodeService _OriginsNodeService=new OriginsNodeService();
        public AntiFakeVerifyService _AntiFakeVerifyService=new AntiFakeVerifyService();

        //get 节点信息
        [ApiVisitFilter("OriginHandleTask")]
        [Route("nbapi/originsTask/origins/getNodelist")]
        [HttpGet]
        public IHttpActionResult GetTempletNodeList(int batchId) 
        {
            var batchModel = _ProductBatchService.GetModelById(batchId);
            var templetNodeList = _OriginsNodeService.GetNodeAll(Convert.ToInt32(batchModel.TempletId), batchId);
            var flowNodeList = _OriginsNodeService.GetNodeAllByFlowId(batchId, 1);
            foreach (var item in templetNodeList)
            {
                item.ExtEnterTimes = 0;//初始录入次数为0
                item.EXTIsEnter = 0;//默认为0为有权限
                item.EXTIsPermission = 0;//初始为0，有权限
                //获取该模板节点 当前已录入次数
                foreach (var inneritem in flowNodeList)
                {
                    if (item.NodeName == inneritem.NodeName)
                    {
                        item.ExtEnterTimes++;
                    }
                }
                if (item.EnterTimes == 1)
                {
                    item.EXTIsEnter = 0;
                }
                else if (item.EnterTimes == 0 && item.ExtEnterTimes > 0)
                {
                    item.EXTIsEnter = 1;
                }
                //判断当前用户是否有该节点录入权限
                if (item.Permission == 1)
                {
                    if (item.PermissionPerson == LogOnUser.UserID)
                    {
                        item.EXTIsPermission = 0;
                    }
                    else
                    {
                        item.EXTIsPermission = 1;
                    }
                }
                }
            return ApiSuccess(templetNodeList);
        }

        //save 节点流程信息
        [ApiVisitFilter("OriginTaskEdit")]
        [Route("nbapi/originstask/origins/addnodes")]
        [HttpPost]
        public IHttpActionResult SaveNodesInfo([FromBody]Origins_Node model)
        {
            model.ChangeDate = DateTime.Now;
            model.ViewDel = 0;
            model.Category = 0;
            model.NodeType=1;//流程
             int result=0; 
            if (model.Id == 0)
            {
            result = _OriginsNodeService.Add(model);
            }
            else
            {
                result = _OriginsNodeService.Update(model);
            }
            if (result>0) return ApiSuccess(true);
            else return ApiSuccess(false);
         }

        [Route("nbapi/originsTask/origins/codesearch")]
        [HttpGet]
        public IHttpActionResult OriginsSearch(string url)
        {
            if (string.IsNullOrEmpty(url)) return ApiFail("无法获取信息");
            int  productId=0;
            string productName = string.Empty;
            string productBatchId = string.Empty;
            string BatchName = string.Empty;
            if (url.IndexOf("code=")!=-1)
            {
              string code=url.Split('=')[1];
           //   string real_fw_code = MiMa.Jiemi(code, AppConst.DigitCodeKey);
              var dt = _AntiFakeVerifyService.GetCodeModel(code, "Wl_Code");
              if (dt == null) return ApiFail("无法获取信息");
               productId =(int) dt.Rows[0]["ProductId"];
               productBatchId = dt.Rows[0]["ProductBatchId"].ToString();
               var model = _ProductBatchService.GetproductByBat(int.Parse(productBatchId));
               productName = model.ProductName;
               BatchName=model.BatchName;
            }
           else if (url.IndexOf("batchId=") != -1)
            {
                productBatchId = url.Split('=')[2];
                var model=_ProductBatchService.GetproductByBat(int.Parse(productBatchId));
                productId=model.Id.Value;
                productName=model.ProductName;
                BatchName = model.BatchName;
            }
            else return ApiFail("无法获取信息");
            return ApiSuccess(new { ProductId = productId, Id = productBatchId, ProductName = productName,BatchName=BatchName, state = true });
        }
        
    }
}
